package com.weight.system.weight.web;

import com.weight.system.weight.PO.WeightInfo;
import com.weight.system.weight.VO.WeightInfoVO;
import com.weight.system.weight.service.WeightInfoService;
import com.weight.system.weight.service.impl.WeightInfoServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 称重记录表
 */
@RestController
@Slf4j
@RequestMapping("/weightInfo")
public class WeightInfoController {

	@Autowired
	private WeightInfoService weightInfoService;

	private String pattern = "yyyy-MM-dd";
	private String pattern2 = "YYYYMMddHHmmss";
	 
	public WeightInfoController(WeightInfoService weightInfoService) {
		this.weightInfoService = weightInfoService;
	}
	
	/**
	 * 1.查询称重记录表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForWeightInfoList",method=RequestMethod.GET)
	public ModelAndView queryForWeightInfoList(){
		
		Map<Object, Object> map =new HashMap<>();
		map.put("weightStatus", 1); 

		
		//2、查询称重记录表列表
		List<WeightInfo> weightInfoList = weightInfoService.findWeightInfoByT(map);
		log.info("weightInfoList={}",weightInfoList);
		List<WeightInfoVO> weightInfoVOList = weightInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		
		
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("weight/queryForWeightInfoList");
			modelAndView.addObject("weightInfoVOList", weightInfoVOList);
			return modelAndView;
	}

	@RequestMapping(value="/weightDataset",method=RequestMethod.GET)
	public ModelAndView weightDataset(){ 
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("weight/weightDataset");
//		modelAndView.addObject("weightInfoVOList", weightInfoVOList);
		return modelAndView;
	}
	 
	@RequestMapping(value="/weightSimple",method=RequestMethod.GET)
	public ModelAndView weightSimple(){ 
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("weight/weightSimple");
//		modelAndView.addObject("weightInfoVOList", weightInfoVOList);
		return modelAndView;
	}
	
	@RequestMapping(value="/weightBarData",method=RequestMethod.GET)
	public ModelAndView weightBarData(){ 
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("weight/weightBarData");
//		modelAndView.addObject("weightInfoVOList", weightInfoVOList);
		return modelAndView;
	}
	/**
	 * 2.查询称重记录表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForWeightInfoListDG",method=RequestMethod.GET)
	public String queryForWeightInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String equmtId = request.getParameter("equmtId");
			String orgId = request.getParameter("orgId");
			String weightDate = request.getParameter("weightDate");
			String weightCount = request.getParameter("weightCount");
			String weightData = request.getParameter("weightData");
			String weightSeq = request.getParameter("weightSeq");
			String weightTime = request.getParameter("weightTime");
			String weightCar = request.getParameter("weightCar");
			String weightOrg = request.getParameter("weightOrg");
			String weightGood = request.getParameter("weightGood");
			String weightMweight = request.getParameter("weightMweight");
			String weightPweight = request.getParameter("weightPweight");
			String weightStatus = request.getParameter("weightStatus");

			Map<Object, Object> map =new HashMap<>();
			map.put("equmtId", equmtId);
			map.put("orgId", orgId);
			map.put("weightDate", DateFormUtils.getStringToDate(weightDate, pattern));
			map.put("weightCount", weightCount);
			map.put("weightData", weightData);
			map.put("weightSeq", weightSeq);
			map.put("weightTime", DateFormUtils.getStringToDate(weightTime, pattern));
			map.put("weightCar", weightCar);
			map.put("weightOrg", weightOrg);
			map.put("weightGood", weightGood);
			map.put("weightMweight", weightMweight);
			map.put("weightPweight", weightPweight);
			map.put("weightStatus", weightStatus);

			
			//2、查询称重记录表列表
			List<WeightInfo> weightInfoList = weightInfoService.findWeightInfoByPage(page,rows,map);
			log.info("weightInfoList={}",weightInfoList);
			List<WeightInfoVO> weightInfoVOList = weightInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<WeightInfoVO> result = new PageResult<WeightInfoVO>(weightInfoVOList);
			return  JSON.toJSONString(result);
	}

	
	
	@Transactional
	@RequestMapping(value="/saveWeightInfoing")
	public JsonResultUtil saveWeightInfoing(String command, String weightings) {
//     	System.out.println(message);
         //记录指令信息表

        //称重指令 记录称重表
        if(StringUtils.equals(command, "0100")) { 
			WeightInfo weightInfo = new WeightInfo(); 
			  
			weightInfo.setWeightData(command);//称重记录数据 	 
			weightInfo.setWeightTime(new Date());//年月日时分秒
			weightInfo.setWeightPweight(weightings);//皮重   
        	weightInfo.setWeightId(CommonUtils.getUUId());
			weightInfo.setCreateBy("000");
			weightInfo.setUpdateBy("000");
			weightInfo.setWeightStatus(1);
			weightInfoService.insertWeightInfo(weightInfo);
	    }
        return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！");
	} 
	
	/**
	 * 展示模板--转--保存模板  
	 * @param weightInfoVO
	 * @param weightInfo
	 * @return
	 */
	public void changeVOToPO(WeightInfoVO weightInfoVO,WeightInfo weightInfo){
			BeanUtils.copyProperties(weightInfoVO,weightInfo);
			weightInfo.setWeightDate(DateFormUtils.getStringToDate(weightInfoVO.getWeightDate(), pattern));
			weightInfo.setWeightTime(DateFormUtils.getStringToDate(weightInfoVO.getWeightTime(), pattern2));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param weightInfoVO
	 * @param weightInfo
	 * @return
	 */
	public void changePoToVO(WeightInfoVO weightInfoVO,WeightInfo weightInfo){
			BeanUtils.copyProperties(weightInfo,weightInfoVO);
			weightInfoVO.setWeightDate(DateFormUtils.getDateToString(weightInfo.getWeightDate(), pattern));
			weightInfoVO.setWeightTime(DateFormUtils.getDateToString(weightInfo.getWeightTime(), pattern2));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param weightInfo
	 * @return
	 */
	public WeightInfoVO changePoToVO(WeightInfo weightInfo){
			WeightInfoVO weightInfoVO=new WeightInfoVO();
			BeanUtils.copyProperties(weightInfo,weightInfoVO);
			weightInfoVO.setWeightDate(DateFormUtils.getDateToString(weightInfo.getWeightDate(), pattern));
			weightInfoVO.setWeightTime(DateFormUtils.getDateToString(weightInfo.getWeightTime(), pattern2));
			

			return weightInfoVO;
	}

	/**
	 * 3.查询称重记录表--详情页
	 * @param weightId
	 * @return
	 */
	@RequestMapping("/queryForWeightInfoDetail")
	public ModelAndView queryForWeightInfoDetail(@RequestParam(value="weightId",required=false) String weightId){
			//1.校验id
			CommonUtils.validateExistID(weightId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("weightId", weightId);
			log.info("查询称重记录表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			WeightInfo weightInfo = weightInfoService.findByWeightInfoId(map);
			
			WeightInfoVO weightInfoVO = changePoToVO(weightInfo);
			//3、返回前台
			log.info("查询称重记录表查询成功：weightInfo={}",weightInfo);
			modelAndView.setViewName("system/weight/queryForWeightInfoDetail");
			modelAndView.addObject("weightInfoVO",weightInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改称重记录表操作
	 * @param weightId
	 * @return
	 */
	@RequestMapping(value="/addWeightInfo")
	public ModelAndView addWeightInfo(@RequestParam(value="weightId",required=false) String weightId){
			//1、创建返回对象
			WeightInfoVO weightInfoVO = new WeightInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(weightId)) {
				Map<String, String> map=new HashMap<>();
				map.put("weightId", weightId);
				log.info("修改操作，查询ID：weightId={}",weightId);
				WeightInfo weightInfo = weightInfoService.findByWeightInfoId(map);
				this.changePoToVO(weightInfoVO,weightInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("system/weight/addWeightInfo");
			modelAndView.addObject("weightInfoVO",weightInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改称重记录表操作
	 * @param weightInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveWeightInfo")
	public JsonResultUtil saveWeightInfo(@Valid WeightInfoVO weightInfoVO){
			String operateStr="add";
			//1.校验入参规则
//			CommonUtils.validateBindingResult(bindingResult,weightInfoVO);
			
			//2.获取id
			String weightId=weightInfoVO.getWeightId();
			WeightInfo weightInfo=new WeightInfo();
			BeanUtils.copyProperties(weightInfoVO,weightInfo);
			this.changeVOToPO(weightInfoVO,weightInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(weightId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("weightId", weightId);
				log.info("修改操作，查询ID：weightId={}",weightId);
				//查询是否存在
				weightInfoService.findByWeightInfoId(map);
				try{
					weightInfo.setUpdateBy("000");
					weightInfoService.updateWeightInfo(weightInfo);
					log.info("更新操作成功：weightInfo={}",weightInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					weightInfo.setWeightId(CommonUtils.getUUId());
					weightInfo.setCreateBy("000");
					weightInfo.setUpdateBy("000");
					weightInfo.setWeightStatus(1);
					weightInfoService.insertWeightInfo(weightInfo);
					log.info("添加操作成功：weightInfo={}",weightInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除称重记录表操作
	 * @param weightId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByWeightInfoId")
	public JsonResultUtil deleteByWeightInfoId(@RequestParam(value="weightId",required=false) String weightId){
			//1.校验id
			CommonUtils.validateExistID(weightId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("weightId", weightId);
			weightInfoService.findByWeightInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除称重记录表入参，map={}",map);
			
			try{
				weightInfoService.upDelByWeightInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除称重记录表操作---批量删除
	 * @param weightIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByWeightInfoIdBatch")
	public JsonResultUtil deleteByWeightInfoIdBatch(@RequestParam(value="weightIdList",required=false) List weightIdList){
			//1.校验id
			CommonUtils.validateExistIDList(weightIdList);
			
			//2.校验数据库
			weightInfoService.findByWeightInfoIdList(weightIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("weightIdList", weightIdList);
			map.put("updateBy","000");
			log.info("批量删除称重记录表操作入参，map={}",map);
			
			try{
				weightInfoService.upDelByWeightInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}